$(function () {

    var json =
        [{
            "name": "韩涛-dn851426212 (五级会员) - 业绩：7795000.00 - 注册日期：2017-05-14 15:38",
            "code":"1",
            "icon": "icon-th",
            "child": [
                {
                    "name": "韩涛-dn218658685 (一级会员) - 业绩：1000.00 - 注册日期：2017-08-01 11:40",
                    "icon": "icon-minus-sign",
                    "code":"11",
                    "parentCode": "1",
                    "child": [
                        {
                            "name": "韩涛-dn283739727 (三级会员) - 业绩：1281000.00 - 注册日期：2017-08-01 11:45",
                            "code":"111",
                            "icon": "",
                            "parentCode": "11",
                            "child": []
                        },
                        {
                            "name": "韩涛-dn283739727 (三级会员) - 业绩：1281000.00 - 注册日期：2017-08-01 11:45",
                            "code":"112",
                            "icon": "",
                            "parentCode": "11",
                            "child": [
                                        {
                                            "name": "韩涛-dn283739727 (三级会员) - 业绩：1281000.00 - 注册日期：2017-08-01 11:45",
                                            "code":"1121",
                                            "icon": "",
                                            "parentCode": "112",
                                            "child": [
                                                        {
                                                            "name": "韩涛-dn283739727 (三级会员) - 业绩：1281000.00 - 注册日期：2017-08-01 11:45",
                                                            "code":"11211",
                                                            "icon": "",
                                                            "parentCode": "1121",
                                                            "child": [
                                                                       {
                                                                            "name": "韩涛-dn283739727 (三级会员) - 业绩：1281000.00 - 注册日期：2017-08-01 11:45",
                                                                            "code":"112111",
                                                                            "icon": "",
                                                                            "parentCode": "11211",
                                                                            "child": []
                                                                        } 
                                                                     ]
                                                        },
                                                        {
                                                            "name": "韩涛-dn283739727 (三级会员) - 业绩：1281000.00 - 注册日期：2017-08-01 11:45",
                                                            "code":"11212",
                                                            "icon": "",
                                                            "parentCode": "112",
                                                            "child": []
                                                        }


                                                     ]
                                        },
                                        {
                                            "name": "韩涛-dn283739727 (三级会员) - 业绩：1281000.00 - 注册日期：2017-08-01 11:45",
                                            "code":"1122",
                                            "icon": "",
                                            "parentCode": "112",
                                            "child": []
                                        }
                                     ]
                        }
                    ]
                },
                {
                    "name": "韩涛-dn896138617 (三级会员) - 业绩：3303000.00 - 注册日期：2017-08-01 11:43",
                    "icon": "",
                    "code":"12",
                    "parentCode": "1",
                    "child": [
                        {
                            "name": "韩涛-dn858779882 (三级会员) - 业绩：1572000.00 - 注册日期：2017-08-01 11:45",
                            "code":"121",
                            "icon": "",
                            "parentCode": "12",
                            "child": []
                        }
                    ]
                },
                {
                    "name": "韩涛-dn551276188 (五级会员) - 业绩：50000.00 - 注册日期：2017-08-01 15:22",
                    "icon": "icon-minus-sign",
                    "code":"13",
                    "parentCode": "1",
                    "child": [
                        {
                            "name": "韩涛-dn283739727 (三级会员) - 业绩：1281000.00 - 注册日期：2017-08-01 11:45",
                            "code":"131",
                            "icon": "",
                            "parentCode": "13",
                            "child": []
                        }
                    ]
                },
                {
                    "name": "韩涛-dn551276188 (五级会员) - 业绩：50000.00 - 注册日期：2017-08-01 15:22",
                    "icon": "icon-minus-sign",
                    "code":"14",
                    "parentCode": "1",
                    "child": [
                        {
                            "name": "韩涛-dn283739727 (三级会员) - 业绩：1281000.00 - 注册日期：2017-08-01 11:45",
                            "code":"141",
                            "icon": "",
                            "parentCode": "14",
                            "child": []
                        }
                    ]
                },
                {
                    "name": "韩涛-dn551276188 (五级会员) - 业绩：50000.00 - 注册日期：2017-08-01 15:22",
                    "icon": "icon-minus-sign",
                    "code":"15",
                    "parentCode": "1",
                    "child": [
                        {
                            "name": "韩涛-dn283739727 (三级会员) - 业绩：1281000.00 - 注册日期：2017-08-01 11:45",
                            "code":"151",
                            "icon": "",
                            "parentCode": "15",
                            "child": []
                        }
                    ]
                }
            ]
        }];


    function tree(data) {
        for (var i = 0; i < data.length; i++) {
            var data2 = data[i];
            if (data[i].icon == "icon-th") {
                $("#rootUL").append("<li data-name='" + data[i].code + "'><span><i class='" + data[i].icon + "'></i> " + data[i].name + "</span></li>");
            } else {
                var children = $("li[data-name='" + data[i].parentCode + "']").children("ul");
                if (children.length == 0) {
                    $("li[data-name='" + data[i].parentCode + "']").append("<ul></ul>")
                }
                $("li[data-name='" + data[i].parentCode + "'] > ul").append(
                    "<li data-name='" + data[i].code + "'>" +
                    "<span>" +
                    "<i class='" + data[i].icon + "'></i> " +
                    data[i].name +
                    "</span>" +
                    "</li>")
            }
            for (var j = 0; j < data[i].child.length; j++) {
                var child = data[i].child[j];
                var children = $("li[data-name='" + child.parentCode + "']").children("ul");
                if (children.length == 0) {
                    $("li[data-name='" + child.parentCode + "']").append("<ul></ul>")
                }
                $("li[data-name='" + child.parentCode + "'] > ul").append(
                    "<li data-name='" + child.code + "'>" +
                    "<span>" +
                    "<i class='" + child.icon + "'></i> " +
                    child.name +
                    "</span>" +
                    "</li>")
                var child2 = data[i].child[j].child;
                tree(child2)
            }
            tree(data[i]);
        }

    }

    tree(json)


});